var SearchData = {
  'page': '0',
  'size': '20',
  'sc_title': '',
};
var base ="";
var Totalempty = 0;
$(function () {
  findList();
  /**
   * 处理标签
   *
   */
  Handlebars.registerHelper('dispose', function (price) {
    return Number(price)/100;
  });
});
/**
 * 查询列表
 * @param page
 */
function findList(page) {
  page = page == null || page == '' ? 0 : page;
  SearchData['page'] = page;
  $.ajax({
    url: base + "/diy/goods",
    type: "GET",
    data: SearchData,
    dataType: "json",
    success: function (resultData) {
      createTable(resultData);
      var searchTotal = resultData.totalElements;
      $(".J_searchTotalCount").text(searchTotal);
      if (searchTotal != Totalempty || searchTotal == 0) {
        Totalempty = searchTotal;
        createPage(resultData);//分页
      }
    },
    error: function () {
      alert("系统异常，请稍后重试！");
    }
  })
}

/**
 * 生成列表
 *
 * @param data
 */

function createTable(data) {
  var myTemplate = Handlebars.compile($("#table-template").html());
  $('#tableList').html(myTemplate(data));
  initClick();
}

/**
 * 生成型号
 *
 * @param data
 */
function createPage(data) {
  var totalCount = data.totalElements, limit = data.size;
  $('#addPager').extendPagination({
    totalCount : totalCount,
    showCount : 5,
    limit : limit,
    callback : function(curr, limit, totalCount) {
      findList(curr - 1);
    }
  });
}
function initClick() {
  $(".a_save a").on("click",function () {
    var setInput = $(this).parents("tr").find("td.custom_price").children("input");
    var goodsId = setInput.attr("id");
    var set_price = Number(setInput.val())*100;
    var price = Number($(this).parents("tr").find(".price_text2 span").text())*100;
    if (set_price === price){
      alert("请重新填写价格!");
      return false;
    }

    if (confirm("是否保存?")){
      var data = {
        "price": set_price
      };
      $.ajax({
        url: "/diy/memberPrice/"+goodsId,
        type: "post",
        data: JSON.stringify(data),
        dataType: "text",
        beforeSend: function(request) {
          request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        },
        success: function (data) {
          if (data === "ok"){
            alert("修改成功!");
            return false;
          }
          alert("操作失败,请稍后重试!");
        },
        error: function (data) {
          alert("操作失败,请稍后重试!");
        }
      })
    }
  })
}
function findByTitle() {
  var title = $(".search_box input").val();
  SearchData['sc_title']=title;
  findList();
}